package com.lynn.common.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

/**
 * DESC:
 * TIME:2019-08-06
 */
public class ViewUtil {

    /**
     * 避免重复点击
     */
    private static long lastClickTime;

    /**
     * 判断是否重复点击
     *
     * @return
     */
    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 1000) {
            return true;
        }
        lastClickTime = time;
        return false;
    }


    /**
     * 设置TextView 的各个方向的图片
     *
     * @param textView TextView
     * @param location 位置 0-left, 1-top, 2-right, 3-bottom, 否则不设置
     * @param resId    资源ID
     */
    public static void setTextViewDrawable(Context c, TextView textView, int resId, int location) {
        setTextViewDrawable(textView, ResUtil.getDrawable(c, resId), location);
    }

    /**
     * 设置TextView 的各个方向的图片
     *
     * @param textView TextView
     * @param drawable 图片对象drawable
     * @param location 位置 0-left, 1-top, 2-right, 3-bottom，否则不设置
     */
    public static void setTextViewDrawable(final TextView textView, Drawable drawable, int location) {
        if (textView == null) return;
        if (location == 0) {
            textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
        } else if (location == 1) {
            textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
        } else if (location == 2) {
            textView.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
        } else if (location == 3) {
            textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
        } else {
            textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }
    }


    public static View createView(Context context, int layoutID) {
        return LayoutInflater.from(context).inflate(layoutID, null);
    }


    /**
     * 测量View的宽高
     *
     * @param view View
     */
    public static void measureWidthAndHeight(View view) {
        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(widthMeasureSpec, heightMeasureSpec);
    }

}
